﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
//using TalesFromTheFishAndChipShop.Components.Enemies;

namespace AGrandVenture
{
    class Generator
    {
        #region Declarations
        private Sprite activeSprite;
        private Sprite inactiveSprite;
        public Vector2 MapLocation;
        public bool Active = true;
        public float LastSpawnCounter = 0;
        public float minSpawnTime = 3.0f;
        public static int enemyTypeToggle = 0;
        public static float maxDistanceFromPlayerToSpawn = 1000;
        #endregion

        #region Constructor
        public Generator(
            Sprite activeSprite,
            Sprite inactiveSprite,
            Vector2 mapLocation)
        {
            MapLocation = mapLocation;
            this.activeSprite = activeSprite;
            this.inactiveSprite = inactiveSprite;
        }
        #endregion

        #region Public Methods
        public bool IsCircleColliding(Vector2 otherCentre, float radius)
        {
            if (!Active)
            {
                return false;
            }
            return activeSprite.IsCircleColliding(otherCentre, radius);
        }

        public void Deactivate()
        {
            Active = false;
        }

        public void Update(GameTime gameTime)
        {
            if (Active)
            {
                float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

                LastSpawnCounter += elapsed;
                if (LastSpawnCounter > minSpawnTime)
                {
                    float distanceFromPlayer = Vector2.Distance(activeSprite.WorldCentre, Player.BaseSprite.WorldCentre);

                    if (distanceFromPlayer > 128 && distanceFromPlayer < maxDistanceFromPlayerToSpawn)
                    {
                        if (EnemyManager.Enemies.Count < EnemyManager.MaxActiveEnemies)
                        {

                            enemyTypeToggle++;
                            if (enemyTypeToggle <= 4 )
                            {
                                EnemyManager.AddEnemy(new GiantBug(activeSprite.WorldLocation));
                            }
                            else if (enemyTypeToggle <= 6)
                            {
                                EnemyManager.AddEnemy(new FlyingOcto(activeSprite.WorldLocation));
                            }
                            else if (enemyTypeToggle <= 8)
                            {
                                EnemyManager.AddEnemy(new SlimeEye(activeSprite.WorldLocation));
                            }
                            else
                            {
                                EnemyManager.AddEnemy(new EvilEye(activeSprite.WorldLocation));
                                enemyTypeToggle = 0;
                            }

                            LastSpawnCounter = 0;
                        }
                    }
                }
                activeSprite.Update(gameTime);
            }
            else
            {
                inactiveSprite.Update(gameTime);
       
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            if (Active)
            {
                activeSprite.Draw(spriteBatch);
            }
            else
            {
                inactiveSprite.Draw(spriteBatch);
            }
        }
        #endregion
    }
}
